
import { RoomModel } from "../../../hall_server/model/RoomModel";
import { RoomPlayer } from "../../../hall_server/model/RoomPlayer";
import { Config } from "../../config/Config";
import { ErrorCode } from "../../config/ErrorCode";
import { ServerConfig } from "../../config/ServerConfig";
import { Logger } from "../utils/Logger";
import { WebSocketClient } from "./WebSocketClient";
import { WebSocketClientManager } from "./WebSocketClientManager";
import { ProtoManager, WsSendData } from "../../lib/net/protobuf/ProtoManager";
import { JhaoProto } from "../../lib/net/protobuf/jhaoproto";

export class SocketClientReqManager {

    public static instance: SocketClientReqManager = new SocketClientReqManager();

    public onSocketMsg(client: WebSocketClient, baseMsg: JhaoProto.BaseMsg) {

    }

    public async req_server_internal_Login(serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let account: string = Config.serverType + "-" + ServerConfig.nowServerConfig.id
        let send_data: JhaoProto.Ireq_server_internal_Login = {
            account: account,
            pass: "sjh666666",
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_Login,
            "en_proto_ctor": JhaoProto.req_server_internal_Login,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_internal_Login", respData.errorCode, respData.errorMsg)
                }
            }
        }
        Logger.log("req_server_internal_Login", send_obj, serverConfig)
        client.sendProto(send_obj);

    }

    public async req_server_internal_kill_all_users(serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let send_data: JhaoProto.Ireq_server_internal_kill_all_users = {
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_kill_all_users,
            "en_proto_ctor": JhaoProto.req_server_internal_kill_all_users,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_internal_kill_all_users", respData.errorCode, respData.errorMsg)
                }
            }
        }
        client.sendProto(send_obj);
    }

    public async req_server_internal_set_server_status(status: number, serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let send_data: JhaoProto.Ireq_server_internal_set_server_status = {
            status: status
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_set_server_status,
            "en_proto_ctor": JhaoProto.req_server_internal_set_server_status,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_internal_set_server_status", respData.errorCode, respData.errorMsg)
                }
            }
        }
        client.sendProto(send_obj);
    }

    public async req_server_internal_get_server_status(serverConfig: ServerConfig): Promise<number> {
        return new Promise((resolve, reject) => {
            let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
            let send_data: JhaoProto.Ireq_server_internal_get_server_status = {
            }
            let send_obj: WsSendData = {
                cmd_index: JhaoProto.CmdIndex.server_internal_get_server_status,
                "en_proto_ctor": JhaoProto.req_server_internal_get_server_status,
                "data": send_data,
                "callback": (respData: JhaoProto.BaseMsg) => {
                    if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                        let protoPack: JhaoProto.resp_server_internal_get_server_status = ProtoManager.instance.decode(JhaoProto.resp_server_internal_get_server_status, respData.data)
                        Logger.log("req_server_internal_get_server_status", protoPack)
                        resolve(protoPack.status)
                    } else {
                        Logger.log("req_server_internal_get_server_status", respData.errorCode, respData.errorMsg)
                        resolve(ServerConfig.Status_Type.Weihu)
                    }
                }
            }
            client.sendProto(send_obj);
        })

    }


    public async req_server_internal_update_online(userids: Array<number>, server_id: number, serverType: string, serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let send_data: JhaoProto.Ireq_server_internal_update_online = {
            userids: userids,
            serverId: server_id,
            serverType: serverType
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_update_online,
            "en_proto_ctor": JhaoProto.req_server_internal_update_online,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_internal_update_online", respData.errorCode, respData.errorMsg)
                }
            }
        }
        client.sendProto(send_obj);
    }

    public async req_server_internal_is_in_server(account: string, serverConfig: ServerConfig): Promise<boolean> {
        return new Promise((resolve, reject) => {
            let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
            let send_data: JhaoProto.Ireq_server_internal_is_in_server = {
                account: account
            }
            let send_obj: WsSendData = {
                cmd_index: JhaoProto.CmdIndex.server_internal_is_in_server,
                "en_proto_ctor": JhaoProto.req_server_internal_is_in_server,
                "data": send_data,
                "callback": (respData: JhaoProto.BaseMsg) => {
                    if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                        let protoPack: JhaoProto.resp_server_internal_is_in_server = ProtoManager.instance.decode(JhaoProto.resp_server_internal_is_in_server, respData.data)
                        resolve(protoPack.isIn)
                    } else {
                        Logger.warn("req_server_internal_is_in_server", respData.errorCode, respData.errorMsg)
                        resolve(false)
                    }
                }
            }
            client.sendProto(send_obj);
        })
    }

    public async req_server_internal_is_user_online(userid: number, serverConfig: ServerConfig): Promise<boolean> {
        return new Promise((resolve, reject) => {
            let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
            let send_data: JhaoProto.Ireq_server_internal_is_user_online = {
                userid: userid
            }
            let send_obj: WsSendData = {
                cmd_index: JhaoProto.CmdIndex.server_internal_is_user_online,
                "en_proto_ctor": JhaoProto.req_server_internal_is_user_online,
                "data": send_data,
                "callback": (respData: JhaoProto.BaseMsg) => {
                    if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                        let protoPack: JhaoProto.resp_server_internal_is_user_online = ProtoManager.instance.decode(JhaoProto.resp_server_internal_is_user_online, respData.data)
                        resolve(protoPack.isOnline)
                    } else {
                        Logger.warn("req_server_is_user_online", respData.errorCode, respData.errorMsg)
                        resolve(false)
                    }
                }
            }
            client.sendProto(send_obj);
        })
    }

    public async req_server_onlie_club(clubid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_onlie_club,
        //     "clubid": clubid,
        // }
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // let respData = await client.sendDataAsync(data);
        // Logger.log("req_server_onlie_club resp=", respData)
        // return respData
    }

    public async req_server_notify_delete_club_user(clubid: number, userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_delete_club_user,
        //     "clubid": clubid,
        //     "userid": userid
        // }
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // let respData = await client.sendDataAsync(data);
        // Logger.log("req_server_notify_delete_club_user resp=", respData)
        // return respData
    }

    public async req_server_notify_set_club_quanxian(clubid: number, userid: number, quanxian: string, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_set_club_quanxian,
        //     "clubid": clubid,
        //     "userid": userid,
        //     "quanxian": quanxian
        // }
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // let respData = await client.sendDataAsync(data);
        // Logger.log("req_server_notify_set_club_quanxian resp=", respData)
        // return respData
    }

    public async req_server_notify_club_freeze(clubid: number, userid: number, user_state: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_club_freeze,
        //     "clubid": clubid,
        //     "userid": userid,
        //     "user_state": user_state
        // }
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // let respData = await client.sendDataAsync(data);
        // Logger.log("req_server_notify_club_freeze resp=", respData)
        // return respData
    }

    public async req_server_notify_set_club_admin(clubid: number, userid: number, actor: number, quanxian: string, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_set_club_quanxian,
        //     "clubid": clubid,
        //     "userid": userid,
        //     "actor": actor,
        //     "quanxian": quanxian
        // }
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // let respData = await client.sendDataAsync(data);
        // Logger.log("req_server_notify_set_club_quanxian resp=", respData)
        // return respData
    }

    public async req_server_insertRoomTemplateModel(serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let send_data: JhaoProto.Ireq_server_internal_club_create_room_template = {
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_club_create_room_template,
            "en_proto_ctor": JhaoProto.req_server_internal_club_create_room_template,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_insertRoomTemplateModel", respData.errorCode, respData.errorMsg)
                }
            }
        }
        client.sendProto(send_obj);
    }

    public async req_server_internal_club_delete_room_template(id: number, serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let send_data: JhaoProto.Ireq_server_internal_club_delete_room_template = {
            id: id,
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_club_delete_room_template,
            "en_proto_ctor": JhaoProto.req_server_internal_club_delete_room_template,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_internal_club_delete_room_template", respData.errorCode, respData.errorMsg)
                }
            }
        }
        client.sendProto(send_obj);
    }

    public async req_server_internal_createRoom(room: JhaoProto.IRoomModel, serverConfig: ServerConfig) {
        let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        let send_data: JhaoProto.Ireq_server_internal_createRoom = {
            room: room
        }
        let send_obj: WsSendData = {
            cmd_index: JhaoProto.CmdIndex.server_internal_createRoom,
            "en_proto_ctor": JhaoProto.req_server_internal_createRoom,
            "data": send_data,
            "callback": (respData: JhaoProto.BaseMsg) => {
                if (respData.errorCode == JhaoProto.ErrorCodeWs.Suc) {
                } else {
                    Logger.warn("req_server_internal_createRoom", respData.errorCode, respData.errorMsg)
                }
            }
        }
        client.sendProto(send_obj);
    }

    public async req_server_joinRoom(room_id: number, player: RoomPlayer, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_joinRoom,
        //     "room_id": room_id,
        //     "player": player
        // }
        // Logger.log("req_server_joinRoom", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_leave_room(room: RoomModel, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_leave_room,
        //     "room": room,
        // }
        // Logger.log("req_server_leave_room", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_deleteRoom(room_id: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_deleteRoom,
        //     "room_id": room_id,
        // }
        // Logger.log("req_server_deleteRoom", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_game_start(room: RoomModel, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_game_start,
        //     "room": room,
        // }
        // Logger.log("req_server_notify_game_start", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_gems(userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_gems,
        //     "userid": userid,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_coins(userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_coins,
        //     "userid": userid,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_income(userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_income,
        //     "userid": userid,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_meili(userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_meili,
        //     "userid": userid,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_lv(userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_lv,
        //     "userid": userid,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_invite(userid: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_invite,
        //     "userid": userid,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_update_img(userid: number, imgurl: string, upload_pic_type: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_update_img,
        //     "userid": userid,
        //     "imgurl": imgurl,
        //     "upload_pic_type": upload_pic_type,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_notify_club_open_or_close(clubid: number, state: number, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_notify_club_open_or_close,
        //     "clubid": clubid,
        //     "state": state,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);
    }

    public async req_server_delete_room_by_hall(room_id: number, nick_name: string, serverConfig: ServerConfig) {
        // let data: object = {
        //     "protoName": ProtoServerConfig.req_server_delete_room_by_hall,
        //     "room_id": room_id,
        //     "nick_name": nick_name,
        // }
        // // Logger.log("req_server_notify_update_gems", data)
        // let client: WebSocketClient = WebSocketClientManager.instance.getWebSocketClientByServerConfig(serverConfig);
        // client.sendDataAsync(data);

    }

}